<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>简易聊天室</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f0f0f0;
            margin: 0;
            padding: 0;
        }
        .chat-container {
            max-width: 600px;
            margin: 50px auto;
            background-color: white;
            padding: 20px;
            border-radius: 10px;
            box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
        }
        .chat-box {
            height: 300px;
            overflow-y: auto;
            border: 1px solid #ddd;
            margin-bottom: 20px;
            padding: 10px;
            border-radius: 10px;
            background-color: #fafafa;
        }
        .message {
            display: flex;
            margin-bottom: 10px;
        }
        .message-content {
            display: flex;
            flex-direction: column;
        }
        .nickname {
            font-weight: bold;
            margin-bottom: 5px;
        }
        .message-text {
            background-color: #e0f7fa;
            padding: 8px;
            border-radius: 8px;
            max-width: 80%;
        }
        .input-box {
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        .input-box input {
            width: 80%;
            padding: 10px;
            margin-right: 10px;
            border: 1px solid #ddd;
            border-radius: 5px;
        }
        .input-box button {
            padding: 10px;
            background-color: #00796b;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
        }
        .input-box button:hover {
            background-color: #004d40;
        }
    </style>
</head>
<body>

<div class="chat-container">
    <div class="chat-box" id="chatBox"></div>
    <div class="input-box">
        <input type="text" id="nickname" placeholder="请输入昵称" />
        <textarea id="messageText" placeholder="请输入消息" rows="3" style="width: 80%; padding: 10px;"></textarea>
        <button onclick="sendMessage()">发送</button>
    </div>
</div>

<script>
    let nicknameSet = false;  // 记录是否已设置昵称

    // 每5秒请求一次新的聊天记录
    setInterval(fetchMessages, 1000);

    // 发送消息
    function sendMessage() {
        const nickname = document.getElementById("nickname").value.trim();
        const messageText = document.getElementById("messageText").value.trim();

        if (!nicknameSet) {
            if (!nickname) {
                alert("请输入昵称！");
                return;
            }
            nicknameSet = true;  // 设置了昵称后
        }

        if (!messageText) {
            alert("请输入消息内容！");
            return;
        }

        // 构建消息数据
        const messageData = {
            nickname,
            messageText
        };

        // 通过 POST 请求将消息发送到服务器
        fetch('/sendMessage', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify(messageData)
        })
        .then(response => response.json())
        .then(data => {
            displayMessage(data);  // 显示服务器返回的消息
        })
        .catch(error => {
            console.error('Error:', error);
        });

        // 清空输入框
        document.getElementById("messageText").value = "";
    }

    // 显示消息
    function displayMessage(data) {
        const chatBox = document.getElementById("chatBox");

        const messageDiv = document.createElement("div");
        messageDiv.classList.add("message");

        const messageContent = document.createElement("div");
        messageContent.classList.add("message-content");

        const nicknameDiv = document.createElement("div");
        nicknameDiv.classList.add("nickname");
        nicknameDiv.textContent = data.nickname;

        const messageText = document.createElement("div");
        messageText.classList.add("message-text");
        messageText.textContent = data.messageText;

        messageContent.appendChild(nicknameDiv);
        messageContent.appendChild(messageText);

        messageDiv.appendChild(messageContent);

        chatBox.appendChild(messageDiv);

        // 滚动到底部
        chatBox.scrollTop = chatBox.scrollHeight;
    }

    // 获取所有消息
    function fetchMessages() {
        fetch('/data.txt')
        .then(response => response.json())
        .then(messages => {
            const chatBox = document.getElementById("chatBox");
            chatBox.innerHTML = ""; // 清空现有聊天框内容

            // 遍历所有消息并显示
            messages.forEach(message => {
                displayMessage(message);
            });
        })
        .catch(error => {
            console.error('Error:', error);
        });
    }
</script>

</body>
</html>
